<?php
namespace App\Http\Logic;

use App\Exceptions\InvalidRequestException;

class BaseLogic
{
    public $user;

    private static $_instance;

    public function __construct()
    {
        // 获取登录的用户信息
        $this->user = request()->user();
    }

    /**
     * 实例化
     * @return mixed|static
     */
    public static function logic()
    {
        $class = get_called_class();
        if (!isset(self::$_instance[$class])) {
            self::$_instance[$class] = new static();
        }
        return self::$_instance[$class];
    }

    /**
     * 组装分页列表
     * @param $data
     * @param $currentPage
     * @param $pageSize
     * @return array
     */
    public function handlePageList($data, $currentPage, $pageSize)
    {
        return [
            'page' => $currentPage,
            'is_end' => count($data) == $pageSize ? false : true,
            'list' => $data
        ];
    }

    /**
     * 抛出异常
     * @param $msg
     * @param int $code
     * @throws InvalidRequestException
     */
    public function throwNewInvalidException($msg, $code = 400)
    {
        throw new InvalidRequestException($msg, $code);
    }
}
